/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pb138.videodesgapps.gui;

import com.google.api.services.drive.model.File;
import cz.muni.fi.pb138.videodesgapps.google.GoogleDriveService;
import cz.muni.fi.pb138.videodesgapps.gui.components.GoogleDriveTreeModel;
import cz.muni.fi.pb138.videodesgapps.gui.components.GoogleDriveTreeRenderer;
import javax.swing.JOptionPane;

/**
 * Tree file chooser dialog for browsing Google Drive file system
 *
 * @author xnevrela
 */
public class GoogleFileChooserDialog extends javax.swing.JDialog {

    public static final int RESULT_CANCEL = 0;
    public static final int RESULT_OK = 1;
    private int result;
    private GoogleDriveService service;

    /**
     * Creates new form GoogleFileChooserDialog
     */
    public GoogleFileChooserDialog(java.awt.Frame parent, boolean modal, GoogleDriveService service) {
        super(parent, modal);
        this.service = service;
        initComponents();
    }

    /**
     * Opens dialog to open file from Google Drive
     * 
     * @return dialog result
     */
    public int showOpenDialog() {
        okButton.setText("Otevřít");
        pathTF.setEnabled(false);

        setVisible(true);

        return result;
    }

    /**
     * Opens dialog to save file to Google Drive
     * 
     * @return dialog result
     */
    public int showSaveDialog() {
        okButton.setText("Uložit");
        pathTF.setEnabled(true);

        setVisible(true);

        return result;
    }

    /**
     * Returns file reference to selected file.
     * 
     * @return file reference to selected file.
     */
    public File getSelectedFile() {
        File result = null;
        Object selectedObject = jTree1.getSelectionPath().getLastPathComponent();
        if (selectedObject != null && selectedObject instanceof File) {
            result = (File) selectedObject;
        }

        return result;
    }

    public String getInsertedFileName() {
        return pathTF.getText();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        jLabel1 = new javax.swing.JLabel();
        pathTF = new javax.swing.JTextField();
        cancelButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Google Drive prohlížeč souborů");

        jTree1.setModel(new GoogleDriveTreeModel(service));
        jTree1.setCellRenderer(new GoogleDriveTreeRenderer());
        jTree1.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                jTree1ValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jTree1);

        jLabel1.setText("Název souboru:");

        cancelButton.setText("Zrušit");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pathTF))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(okButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(pathTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(okButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTree1ValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_jTree1ValueChanged
        StringBuilder sb = new StringBuilder();
        File file;


        Object partOfPath = evt.getPath().getLastPathComponent();
        if (partOfPath instanceof File) {
            file = (File) partOfPath;
            if (!service.isFolder(file)) {
                pathTF.setText(file.getTitle());
            }
        }

        
    }//GEN-LAST:event_jTree1ValueChanged

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        result = RESULT_CANCEL;
        this.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        if (pathTF.getText().trim().isEmpty()) {
            int dialogResult = JOptionPane.showConfirmDialog(this, "Nebyl vybrán žádný soubor. Opravdu zrušit?", "Chyba", JOptionPane.YES_NO_OPTION);
            if (dialogResult == JOptionPane.YES_OPTION) {
                result = RESULT_CANCEL;
                this.setVisible(false);
            }
        } else {
            result = RESULT_OK;
            this.setVisible(false);
        }
    }//GEN-LAST:event_okButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTree jTree1;
    private javax.swing.JButton okButton;
    private javax.swing.JTextField pathTF;
    // End of variables declaration//GEN-END:variables
}
